package route

import (
	"encoding/json"
	"image/png"
	"net/http"
	"upload-demo/imghand"
	"upload-demo/server"
	"upload-demo/uphand"
)

// 注册访问路由
func InitRoute() {
	// 路由处理绑定
	server.Handle("/", uphand.Controller{})

	// 获取图片信息
	server.HandleFunc("/info", uphand.Info)

	// 测试上传
	server.HandleFunc("/test", Test)

	// 获取状态码
	server.HandleFunc("/statuscode", StatusCode)

	server.HandleFunc("/blankImg", blankImg)
}

func blankImg(w http.ResponseWriter, r *http.Request) {
	// 图片流方式输出
	w.Header().Set("Content-Type", "image/png")
	// 进行图片的编码
	png.Encode(w, imghand.NoImg)
}

func StatusCode(w http.ResponseWriter, r *http.Request) {
	data, _ := json.Marshal(uphand.GetStatusText())
	w.Write(data)
}

// 测试上传
func Test(w http.ResponseWriter, r *http.Request) {
	html := `
<html>
    <head>
        <title>测试上传</title>
    </head>
    <body>
        <div style="padding: 20px;">
            <p>测试上传</p>
            <form action="/" method="post" enctype="multipart/form-data">
                <p><input type="file" name="userfile" /></p>
                <p><input type="submit" name="submit" value="上传" /></p>
            </form>
        </div>
    </body>
</html>`

	w.Write([]byte(html))
}
